package cn.kgc.crowdfundingadminspringboot.controller;


import cn.kgc.crowdfundingadminspringboot.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot.pojo.Advertisement;
import cn.kgc.crowdfundingadminspringboot.pojo.Message;
import cn.kgc.crowdfundingadminspringboot.service.MessageService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Resource
    MessageService messageService;

    @RequestMapping("/to/message.html")
    public String toPage(){
        return "message-page";
    }

    @ResponseBody
    @RequestMapping("/search/by/keyword")
    public ResultEntity<PageInfo<Message>> search(
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword
    ) {

        // 1.查询得到PageInfo对象
        PageInfo<Message> pageInfo = messageService.select(pageNum, pageSize,keyword);

        // 2.封装结果对象返回
        return ResultEntity.successWithoutData(pageInfo);
    }


    @ResponseBody
    @RequestMapping("/update/message")
    public ResultEntity<String> updateRole(Message message) {

        messageService.updateAdvertisement(message);

        return ResultEntity.successWithoutData();
    }


}
